const http = require('../../../../../utils/http.js')
const CHARTS = require('../../../../../utils/wxcharts-min.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        navBar: {
            title: '测试记录',
            btn: 'goBack'
        }
    },

    /**
     * 返回
     */
    goBack() {
        http.goBack()
    },

    /**
     * 柱状图显示
     */
    columnShow: function (total_score, categories, score) {
        let that = this,
            column = {
                canvasId: 'columnCanvas',
                type: 'column',
                animation: true,
                dataLabel: true,
                legend: false,
                categories: categories,
                series: [{
                    name: '分数',
                    data: score,
                    color: '#02bf65'
                }],
                yAxis: {
                    format: function (val) {
                        return val
                    },
                    min: 0,
                    max: total_score,
                    // title: '分数',
                    gridColor: '#fff',
                    fontColor: '#333'
                },  // Y轴配置
                xAxis: {
                    gridColor: '#fff',
                    // disableGrid: false,
                    type: 'calibration',
                    fontColor: '#333'
                },  // X轴配置
                width: 345,
                height: 267,
                extra: {
                    column: {
                        width: 15
                    }
                }
            }
        new CHARTS(column)
    },

    /**
     * 获取测试记录
     */
    getMytestLog(id) {
        let that = this,
            url = 'Test_mytestLog',
            token = wx.getStorageSync('token')

        http._post(
            url,
            { token: token, ltid: id },
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    let temp_arr = data.data.list,
                        categories = [],
                        score = []
                        
                    if (temp_arr.length <= 4) {
                        for (let item of temp_arr) {
                            categories.push(item.addtime)
                            score.push(item.score)
                        }
                    } else {
                        for (let i = (temp_arr.length-4); i < temp_arr.length; i++) {
                            categories.push(temp_arr[i].addtime)
                            score.push(temp_arr[i].score)
                        }
                    }
                    that.setData({
                        total_score: data.data.score
                    })
                    that.columnShow(Number(data.data.score), categories, score)
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let ltid = options.ltid
        this.getMytestLog(ltid)  // 获取测试记录
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})